import paddle
from paddle.nn import Linear
import numpy as np


class Regressor(paddle.nn.Layer):

    def __init__(self):
        # 初始化父类中的一些参数
        super(Regressor,self).__init__()
        # 定义一层全连接层，输入维度是13，输出维度是1
        self.fc = Linear(in_features=13,out_features=1)

    # 网络的前向计算
    def forward(self,inputs):
        x = self.fc(inputs)
        return x


paddle.set_default_dtype("float32")

# 使用飞浆高层API加载波士顿房价预测数据集，包含训练集和测试集
train_dataset = paddle.text.datasets.UCIHousing(mode='train')
eval_dataset = paddle.text.datasets.UCIHousing(mode='test')
print(eval_dataset)

# 模型训练
model = paddle.Model(Regressor())
model.prepare(paddle.optimizer.SGD(learning_rate=0.005,parameters=model.parameters()),paddle.nn.MSELoss())
model.fit(train_dataset,eval_dataset,epochs=10,batch_size=10,verbose=1)

result = model.evaluate(eval_dataset,batch_size=10)

result_pred = model.predict(eval_dataset,batch_size=1)
result_pred = result_pred[0]
print("Inference result is {}, the corresponding label is {}".format(result_pred[0][0],0))